package com.yxr.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxr.constants.SystemCanstants;
import com.yxr.domain.ResponseResult;
import com.yxr.domain.entity.Comment;
import com.yxr.domain.entity.User;
import com.yxr.domain.exception.SystemException;
import com.yxr.domain.vo.CommentVo;
import com.yxr.domain.vo.PageVo;
import com.yxr.enums.AppHttpCodeEnum;
import com.yxr.mapper.CommentMapper;
import com.yxr.service.CommentService;
import com.yxr.service.UserService;
import com.yxr.utils.BeanCopyUtils;
import com.yxr.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 评论表(Comment)表服务实现类
 *
 * @author makejava
 * @since 2024-07-08 19:18:28
 */
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {

    @Autowired
    private UserService userService;

    @Override
    public ResponseResult commentList(String commentType,Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的根评论

        //articleid判断
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        //当查询的为友链评论时不需要判断文章id负责为Null值
        queryWrapper.eq(SystemCanstants.ARTICLE_COMMENT.equals(commentType),Comment::getArticleId,articleId);
        //根评论id为-1
         queryWrapper.eq(Comment::getRootId,-1);
         //评论类型
        queryWrapper.eq(Comment::getType,commentType);
        //分页查询
        Page<Comment> page = new Page<>(pageNum,pageSize);
        page(page,queryWrapper);
//        获取一页数据
//        page.getRecords();
       // List<CommentVo> commentVoList = BeanCopyUtils.copyBeanaList(page.getRecords(),CommentVo.class);
        List<CommentVo> commentVoList1 = toCommentVoList(page.getRecords());

        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        for (CommentVo commentVo : commentVoList1) {
            //查询对应的子评论
           List<CommentVo> children = getChildren(commentVo.getId());
           commentVo.setChildren(children);
        }


        return ResponseResult.okResult(new PageVo(commentVoList1,page.getTotal()));
    }

    @Override
    public ResponseResult addComment(Comment comment) {
        //MyMetaObjectHandler类自动注入

         //评论不能为空
        if(!StringUtils.hasText(comment.getContent())){
            throw new SystemException(AppHttpCodeEnum.CONTENT_NOT_NULL);
        }
        save(comment);


        return null;
    }

    /**
     *完成comment和commentVo的转换，给昵称赋值
     * @param list
     * @return
     */
    private List<CommentVo> toCommentVoList(List<Comment> list){
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanaList(list, CommentVo.class);
        for (CommentVo commentVo:commentVos){

            // 遍历vo集合
            String nickName = userService.getById(commentVo.getCreateBy()).getNickName();
            String avator = userService.getById(commentVo.getCreateBy()).getAvatar();
            commentVo.setUsername(nickName);
            commentVo.setAvatar(avator);
            //通过creatBy查询用户昵称并且赋值
            //如果tocommentUserid不为-1才进行查询
            if(commentVo.getToCommentUserId()!=-1){
                String toCommentNickName = userService.getById(commentVo.getToCommentUserId()).getNickName();
                String ava =userService.getById(commentVo.getCreateBy()).getAvatar();
                commentVo.setToCommentUserName(toCommentNickName);
                commentVo.setAvatar(ava);
            }
        }

        //通过toCommentUserId查询用户的昵称并赋值
         return commentVos;
    }

    //根据根评论的id查询对应子评论的集合
    private List<CommentVo> getChildren(Long id){
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getRootId,id);
        queryWrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> list = list(queryWrapper);

        List<CommentVo> commentVoList = toCommentVoList(list);
       return commentVoList;
    }

}
